package com.zkthink.ceres.app.common.controller;


import com.zkthink.base.R;
import com.zkthink.base.controller.SuperController;
import com.zkthink.ceres.authority.dto.common.DictionarySaveDTO;
import com.zkthink.ceres.authority.dto.common.DictionaryUpdateDTO;
import com.zkthink.ceres.authority.entity.common.Dictionary;
import com.zkthink.ceres.authority.entity.common.DictionaryItem;
import com.zkthink.ceres.authority.service.common.DictionaryItemService;
import com.zkthink.ceres.authority.service.common.DictionaryService;
import com.zkthink.ceres.common.annotations.LoginMode;
import com.zkthink.database.mybatis.conditions.Wraps;
import com.zkthink.security.annotation.PreAuth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * 字典类型
 * </p>
 *
 * @author ceres
 * @date 2019-07-22
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/dict")
@Api(value = "dict", tags = "字典服务")
@LoginMode(strict = false)
public class DictionaryController extends
    SuperController<DictionaryService, Long, Dictionary, Dictionary, DictionarySaveDTO, DictionaryUpdateDTO> {

    @Autowired
    private DictionaryItemService dictionaryItemService;

    @ApiOperation(value = "根据字典类型查询明细项", notes = "查询字典明细")
    @GetMapping("/queryDictionaryItem/{type}")
    public R queryDictionaryItem(@PathVariable("type") String type){
        return R.success(dictionaryItemService.list(Wraps.<DictionaryItem>lbQ()
                .eq(DictionaryItem::getDictionaryType,type)
                .eq(DictionaryItem::getStatus,true)
                .orderByAsc(DictionaryItem::getSortValue)));
    }

    @Override
    public R<Boolean> handlerDelete(List<Long> ids) {
        this.baseService.removeByIds(ids);
        this.dictionaryItemService.remove(Wraps.<DictionaryItem>lbQ().in(DictionaryItem::getDictionaryId, ids));
        return this.success(true);
    }

}
